/****************************************************/
*Fairness: Data Cleaning							*/
*Date Created: Jan 29, 2017							*/
*Notes: Read in and clean data for part 1 of 		*/
*		fairness experiment							*/
/****************************************************/


clear all
set more off

/****************************************/
/*				PREAMBLE	 			*/
/****************************************/

*Set directories: Change local directory paths
global raw_dir "<INSERT RAW DATA DIRECTORY HERE>"
global out_dir "<INSERT OUTPUT DIRECTORY HERE>"
cd "$raw_dir/src"


/****************************************/
/*		READ IN DATA: PART 1			*/
/****************************************/
*Import xlsx files
local filenames 120509_1658_NK 120509_1855_BK 120514_1712_NK 120516_1646_SK ///
				120522_1938_CI 120524_1736_BK 120529_1820_SK 120604_1909_CI ///
				120614_1120 120614_1248 120614_1356 120615_1109 120615_1234 ///
				120615_1419 120618_1149 120618_1350 120619_1130 120619_1343 ///
				120620_1136 120620_1345

foreach excelfile in `filenames' {
				
import excel using `excelfile'.xlsx 

*rename
rename A sessionfile
rename B part
rename C datatype
rename D period
rename E subjectnum
rename F group
rename G profit
rename H totalprofit
rename I participate
rename J role
rename K makeorder
rename L order
rename M TimeCONTINUEConsentFormOK
rename N TimeOKInstructionsOK
rename O value
rename P cost
rename Q othercost
rename R othervalue
rename S TimeOKRoleIntroductionOK
rename T offer
rename U TimeOKScreen1OK
rename V accept
rename W TimeAcceptScreen2OK
rename X TimeRejectScreen2OK
rename Y seller
rename Z earnings
rename AA otherearnings
rename AB TimeOKScreen3OK

ds
di c(k)
local lastvar: word `c(k)' of `r(varlist)'
display "`lastvar'"

drop AC-`lastvar'
keep if part==1
keep if datatype=="subjects"
drop if period=="Period"
drop profit totalprofit makeorder order datatype participate seller

*add labels:
label variable sessionfile "Session file number"
label variable part "Experiment part; 4 parts total"
label variable period "Period; 30 periods total"
label variable subjectnum "zTree subject number"
label variable group "zTree group identifies pairs"
label variable role "Role of subject; Buyer==1, Seller==2"
label variable value "Value of Buyer; Randomly selected 70 or 90"
label variable cost "Cost of Seller; Randomly selected 10 or 30"
label variable othercost "Cost of seller when subject is a buyer"
label variable othervalue "Value of buyer when subject is a seller"
label variable offer "Buyer's offer"
label variable accept "Seller accept or reject; accept==1, otherwise 0"
label variable earnings "Buyer and seller earnings"
label variable otherearnings "Partner's earnings"

destring sessionfile-otherearnings, replace

save "$out_dir/`excelfile'_part1", replace 
clear 
}



/****************************************/
/*				APPEND DATA 			*/
/****************************************/

cd "$out_dir"

*Append data
use 120509_1658_NK_part1 , replace
append using 120509_1855_BK_part1, force
append using 120514_1712_NK_part1, force
append using 120516_1646_SK_part1, force
append using 120522_1938_CI_part1, force
append using 120524_1736_BK_part1, force
append using 120529_1820_SK_part1, force
append using 120604_1909_CI_part1, force
append using 120614_1120_part1, force
append using 120614_1248_part1, force
append using 120614_1356_part1, force
append using 120615_1109_part1, force
append using 120615_1234_part1, force
append using 120615_1419_part1, force
append using 120618_1149_part1, force
append using 120618_1350_part1, force
append using 120619_1130_part1, force
append using 120619_1343_part1, force
append using 120620_1136_part1, force
append using 120620_1345_part1, force

replace TimeRejectScreen2OK = "" if TimeRejectScreen2OK == "-"
destring(TimeRejectScreen2OK), replace
gen sellerresponse = . 
	replace sellerresponse = TimeRejectScreen2OK if accept == 0
	replace sellerresponse = TimeAcceptScreen2OK if accept == 1
gen buyerresponse = TimeOKScreen1OK
save fairness_part1, replace

/****************************************/
/*				CLEAN DATA	 			*/
/****************************************/
use fairness_part1, replace

*assign session number
gen session = .
	replace session= 1  if sessionfile == "120509_1658"
	replace session= 2  if sessionfile == "120509_1855"
	replace session= 3  if sessionfile == "120514_1712"
	replace session= 4  if sessionfile == "120516_1646"
	replace session= 5  if sessionfile == "120522_1938"
	replace session= 6  if sessionfile == "120524_1736"
	replace session= 7  if sessionfile == "120529_1820"
	replace session= 8  if sessionfile == "120604_1909"
	replace session= 9  if sessionfile == "120614_1120"
	replace session= 10  if sessionfile == "120614_1248"
	replace session= 11  if sessionfile == "120614_1356"
	replace session= 12  if sessionfile == "120615_1109"
	replace session= 13  if sessionfile == "120615_1234"
	replace session= 14  if sessionfile == "120615_1419"
	replace session= 15  if sessionfile == "120618_1149"
	replace session= 16  if sessionfile == "120618_1350"
	replace session= 17  if sessionfile == "120619_1130"
	replace session= 18  if sessionfile == "120619_1343"
	replace session= 19  if sessionfile == "120620_1136"
	replace session= 20  if sessionfile == "120620_1345"
label variable session "Session number"

*assign treatment number (based on global datatype) - see codebook
gen treatment = .	
	replace treatment= 1  if sessionfile == "120509_1658"
	replace treatment= 3  if sessionfile == "120509_1855"
	replace treatment= 1  if sessionfile == "120514_1712"
	replace treatment= 2  if sessionfile == "120516_1646"
	replace treatment= 4  if sessionfile == "120522_1938"
	replace treatment= 3  if sessionfile == "120524_1736"
	replace treatment= 2  if sessionfile == "120529_1820"
	replace treatment= 4  if sessionfile == "120604_1909"
	replace treatment= 4  if sessionfile == "120614_1120"
	replace treatment= 1  if sessionfile == "120614_1248"
	replace treatment= 3  if sessionfile == "120614_1356"
	replace treatment= 1  if sessionfile == "120615_1109"
	replace treatment= 2  if sessionfile == "120615_1234"
	replace treatment= 4  if sessionfile == "120615_1419"
	replace treatment= 2  if sessionfile == "120618_1149"
	replace treatment= 3  if sessionfile == "120618_1350"
	replace treatment= 3  if sessionfile == "120619_1130"
	replace treatment= 1  if sessionfile == "120619_1343"
	replace treatment= 4  if sessionfile == "120620_1136"
	replace treatment= 2  if sessionfile == "120620_1345"
label variable treatment "Treament: 1==NK; 2==SK; 3==BK; 4==CI"

*generate subject id based on session and subject number	
gen subj_id = session*100 + subjectnum
label variable subj_id "Subject ID"

*replace value=. for sellers and cost=. for buyers and vice versa for othervalue and othercost
replace value=. if role==2
replace cost=. if role==1
replace othervalue=. if role==1
replace othercost=. if role==2

*generate dummy for buyer
gen buyer=0
	replace buyer=1 if role==1
label variable buyer "Buyer==1, otherwise 0"


save fairness_part1, replace

*End Code



